<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>模型剪裁 |Mars3D|三维地图| 火星科技|合肥火星科技|合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,bootstrap-checkbox,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <input type="button" class="btn btn-primary" value="绘制矩形" id="btnDrawExtent" />
      <input type="button" class="btn btn-primary" value="绘制剪裁区" id="btnDraw" />
      <input type="button" class="btn btn-primary" value="清除" id="clearWJ" />

      <div class="radio radio-info radio-inline">
        <input type="radio" id="rdoClipType1" name="rdoClipType" value="0" checked />
        <label for="rdoClipType1">内裁剪</label>
      </div>
      <div class="radio radio-info radio-inline">
        <input type="radio" id="rdoClipType2" name="rdoClipType" value="1" />
        <label for="rdoClipType2">外裁剪 </label>
      </div>
    </div>

    <div class="infoview info-tips">
      提示：<br />
      (1) 当前只支持单个压平<br />
      (2) 对3dtiles数据有要求，仅适用于无自带着色器的模型<br />
      (3) 每次切换视角需要模型加载完成后才去裁剪<br />
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      'use script' //开发环境建议开启严格模式

      var map
      var tilesetClip

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.util.merge(options, {
          scene: {
            center: {
              y: 36.534035,
              x: 101.494838,
              z: 2871.39,
              heading: 196.6,
              pitch: -43.5,
              roll: 359.9,
            },
          },
        })

        //创建三维地球场景
        map = new mars3d.Map('mars3dContainer', mapOptions)

        //加模型
        var tilesetLayer = new mars3d.layer.TilesetLayer({
          url: 'http://data.marsgis.cn/3dtiles/qx-shuiwu/tileset.json',
          maximumScreenSpaceError: 2,
          maximumMemoryUsage: 1024,
          offset: {
            z: -100,
          },
          center: {
            y: 36.534035,
            x: 101.494838,
            z: 2871.39,
            heading: 196.6,
            pitch: -43.5,
            roll: 359.9,
          },
          flyTo: true,
        })
        map.addLayer(tilesetLayer)

        //会执行多次，重新加载一次完成后都会回调
        tilesetLayer.on(mars3d.EventType.allTilesLoaded, function (event) {
          console.log('触发allTilesLoaded事件', event)
        })

        //模型开挖处理类
        tilesetClip = new mars3d.thing.TilesetClip({
          layer: tilesetLayer,
          positions: [
            [101.494598, 36.531232, 2575.68],
            [101.493713, 36.531463, 2575.83],
            [101.493934, 36.532096, 2576.18],
            [101.494881, 36.531865, 2575.58],
          ],
          outline: true, //显示边线
        })
        map.addThing(tilesetClip)

        $('input:radio[name="rdoClipType"]').change(function () {
          if ($(this).val() === '1') {
            tilesetClip.clipOutSide = true
          } else {
            tilesetClip.clipOutSide = false
          }
        })

        $('#clearWJ').click(function () {
          graphicLayer.clear()
          tilesetClip.clear()
        })

        var graphicLayer = new mars3d.layer.GraphicLayer({
          hasEdit: true,
          isAutoEditing: true, //绘制完成后是否自动激活编辑
        })
        map.addLayer(graphicLayer)

        graphicLayer.on(mars3d.EventType.drawCreated, function (e) {
          startEditing(e.graphic)
        })
        graphicLayer.on(mars3d.EventType.editMovePoint, function (e) {
          startEditing(e.graphic)
        })
        graphicLayer.on(mars3d.EventType.editRemovePoint, function (e) {
          startEditing(e.graphic)
        })
        graphicLayer.on(mars3d.EventType.editStop, function (e) {
          startEditing(e.graphic)
        })

        $('#btnDraw').click(function () {
          graphicLayer.clear()
          graphicLayer.startDraw({
            type: 'polygon',
            style: {
              color: '#007be6',
              opacity: 0.2,
              outline: false,
            },
          })
        })
        $('#btnDrawExtent').click(function () {
          graphicLayer.clear()
          graphicLayer.startDraw({
            type: 'rectangle',
            style: {
              color: '#007be6',
              opacity: 0.2,
              outline: false,
            },
          })
        })
      }

      function startEditing(graphic) {
        var positions = graphic.positionsShow
        if (graphic.getOutlinePositions) {
          //rectangle时
          positions = graphic.getOutlinePositions(true)
        }

        //赋值绘制的边界坐标
        tilesetClip.positions = positions

        console.log(JSON.stringify(mars3d.transform.cartesians2lonlats(positions))) //打印下边界
      }
    </script>
  </body>
</html>
